<?php

/**
 * Fonction simplifiant les redirections
 */
function redirect($url){
	header("Location:".$url);
}

/**
 * Fonction permettant de v�rifier si l'utilisateur est authentifi� 
 */
function isAuthenticated(){
	if (isset($_SESSION['isAuthenticated']) &&  $_SESSION['isAuthenticated']){
		return true;
	}
}

/**
 * Fonction permettant de v�rifier si l'utilisateur est administrateur 
 */
function isAdmin(){
	if (!isset($_SESSION['admin'])){
		return true;
	}
}

/**
 * Retourne le nom du controller � charger
 */
function getController(){

	global $aliases;
	$controller = array();
	
	$pathinfo = pathinfo($_SERVER['QUERY_STRING']);
	$controller['uri'] = $pathinfo['filename'];
	
	// Si the query string est vide il s'agit de l'index
	if ($controller['uri'] == "" || $controller['uri'] == "&"){
		$controller['uri'] = "index";
	} 
	
	// Si l'URL contient des param�tres, on extrait que la premi�re partie
	$containsParam = strpos($controller['uri'], '&');
	if ($containsParam){
		$controller['name'] = substr($controller['uri'], 0, $containsParam);
	} else {
		$controller['name'] = $controller['uri'];
	}
	
	
	return $controller;
}

/**
 * Retourne le chemin du controller 
 */
function getControllerPath($controller){
	return $controller_file = MM_CONTROLLER_PATH.$controller['name'].'.php';
}

/**
 * Retourne le chemin du controller 
 */
function getViewPath($controller){
	return $_file = MM_TEMPLATE_PATH.$controller['uri'].'.html';
}

/**
 * Cette fonction retourne un tableau de timestamp correspondant
 * aux jours fériés en France pour une année donnée.
 */
function isWorkableDay( $date = null ) {

    if ($date === null) {
        $date = time();
    }
    $date = strtotime(date('m/d/Y', $date));

    $weekDay = date('w', $date);
    if($weekDay == 0 || $weekDay == 6){
        return false;
    }
    
    $year = date('Y', $date);

    $easterDate = easter_date($year);
    $easterDay = date('j', $easterDate);
    $easterMonth = date('n', $easterDate);
    $easterYear = date('Y', $easterDate);

    $holidays = array(
        // Dates fixes
        mktime(0, 0, 0, 1, 1, $year), // 1er janvier
        mktime(0, 0, 0, 5, 1, $year), // Fête du travail
        mktime(0, 0, 0, 5, 8, $year), // Victoire des alliés
        mktime(0, 0, 0, 7, 14, $year), // Fête nationale
        mktime(0, 0, 0, 8, 15, $year), // Assomption
        mktime(0, 0, 0, 11, 1, $year), // Toussaint
        mktime(0, 0, 0, 11, 11, $year), // Armistice
        mktime(0, 0, 0, 12, 25, $year), // Noel
        // Dates variables
        mktime(0, 0, 0, $easterMonth, $easterDay + 1, $easterYear),
        mktime(0, 0, 0, $easterMonth, $easterDay + 39, $easterYear),
        mktime(0, 0, 0, $easterMonth, $easterDay + 50, $easterYear),
    );

    return !in_array($date, $holidays);
}

